home *** CD-ROM | disk | FTP | other *** search
- //a_stream.h - output classes
-
- ///////////////////////////////////////////////////////////////////////////////////
- // AStreamOutput class, includes encryption
- ///////////////////////////////////////////////////////////////////////////////////
- class AStreamOutput : public ABase
- {
- public:
- AStreamOutput(ostream *posOut = NULL) { m_posOut = (posOut ? posOut : &cout); }
- ~AStreamOutput() {};
-
- //a_Declares debug/dump related functions
- #ifdef _DEBUG_DUMP_
- void dump(void); //a_Debugging dump, when _DEBUG_DUMP_ is set
- #endif
-
- //a_Output to ostream variable m_posOutput, by default cout
- void setOStream(ostream *posNewOut)
- {
- if (posNewOut) m_posOut = posNewOut;
- }
- ostream *getOStream(void) { return m_posOut; }
-
- //a_Standard stream support with assert() checks
- AStreamOutput &operator<< (char cOut) { assert(m_posOut); *m_posOut << cOut; return *this; }
- AStreamOutput &operator<< (short nOut) { assert(m_posOut); *m_posOut << nOut; return *this; }
- AStreamOutput &operator<< (long lOut) { assert(m_posOut); *m_posOut << lOut; return *this; }
- AStreamOutput &operator<< (BYTE bOut) { assert(m_posOut); *m_posOut << bOut; return *this; }
- AStreamOutput &operator<< (WORD wOut) { assert(m_posOut); *m_posOut << wOut; return *this; }
- AStreamOutput &operator<< (DWORD dwOut) { assert(m_posOut); *m_posOut << dwOut; return *this; }
- AStreamOutput &operator<< (int iOut) { assert(m_posOut); *m_posOut << iOut; return *this; }
- AStreamOutput &operator<< (UINT uOut) { assert(m_posOut); *m_posOut << uOut; return *this; }
- AStreamOutput &operator<< (float fOut) { assert(m_posOut); *m_posOut << fOut; return *this; }
- AStreamOutput &operator<< (double dOut) { assert(m_posOut); *m_posOut << dOut; return *this; }
- AStreamOutput &operator<< (const char *pccOut)
- { assert(m_posOut); outString(pccOut); return *this; }
- AStreamOutput &operator<< (signed char scOut)
- { assert(m_posOut); *m_posOut << scOut; return *this; }
- AStreamOutput &operator<< (void *pv)
- { assert(m_posOut); *m_posOut << pv; return *this; }
- AStreamOutput &operator<< (streambuf *psb)
- { assert(m_posOut); *m_posOut << psb; return *this; }
- AStreamOutput &operator<< (ostream& (*fcn)(ostream&))
- { assert(m_posOut); *m_posOut << fcn; return *this; }
- AStreamOutput &operator<< (ios& (*fcn)(ios&))
- { assert(m_posOut); *m_posOut << fcn; return *this; }
-
- //a_Stream wrappers
- void outString(const char *pccOut)
- {
- assert(m_posOut);
- if (pccOut)
- *m_posOut << pccOut;
- else
- *m_posOut << "(null)";
- }
- void outStringN(const char *pccOut) //a_For speed does no check
- {
- assert(m_posOut);
- *m_posOut << pccOut;
- }
- void outStringQ(const char *pccOut) //a_Add quotes
- {
- assert(m_posOut);
- if (pccOut)
- *m_posOut << "\"" << pccOut << "\"";
- else
- *m_posOut << "(null)";
- }
- void outStringCR(const char *pccOut)
- {
- assert(m_posOut);
- if (pccOut)
- *m_posOut << pccOut;
- else
- *m_posOut << "(null)";
-
- *m_posOut << endl; //a_With end-of-line added
- }
- void outStringCRN(const char *pccOut)
- {
- assert(m_posOut);
- *m_posOut << pccOut << endl;
- }
-
- protected:
- ostream *m_posOut;
-
- //a_Raw dump to the output stream (what ever it was set to)
- };
-
-
- ///////////////////////////////////////////////////////////////////////////////////
- // AHTML class
- ///////////////////////////////////////////////////////////////////////////////////
- class AHTML : public AStreamOutput
- {
- public:
- AHTML(ostream *posOut = NULL) : AStreamOutput(posOut) {}
- virtual ~AHTML() {}
-
- //a_Declares debug/dump related functions
- #ifdef _DEBUG_DUMP_
- void dump(void); //a_Debugging dump, when _DEBUG_DUMP_ is set
- #endif
-
- //a_NOTE: \n added to make output readable to the user and is not needed for HTML
- //a_HERE IS A BASIC HTML DOCUMENT (Note: Must send the MIME directive first from a CGI)
- //a_<HTML>
- //a_<HEAD><TITLE>My Title</TITLE></HEAD>
- //a_<BODY>
- //a_</BODY>
- //a_</HTML>
-
- //a_HTML start & end
- void htmlStartHTML(void) { outStringCRN("<HTML>"); };
- void htmlEndHTML(void) { outStringCRN("</HTML>"); };
-
- //a_HEAD section and TITLE; htmlDoHEAD() is all that is really needed at this time
- void htmlStartHEAD(void) { outStringCRN("<HEAD>"); };
- void htmlEndHEAD(void) { outStringCRN("</HEAD>"); };
- void htmlDoHEAD(const char *pccTitle, const char *pccExtra = NULL);
-
- void htmlStartTITLE(void) { outStringCRN("<TITLE>"); };
- void htmlEndTITLE(void) { outStringCRN("</TITLE>"); };
- void htmlDoTITLE(const char *pccTitle);
-
- //a_META directive
- void htmlDoMETA(const char *pccName, const char *pccContent);
-
- //a_BODY directives
- void htmlStartBODY(COLORREF crBack = 0x00808080, COLORREF crText = 0x00FFFFFF,
- COLORREF crLink = 0x0000DDDD, COLORREF crALink = 0x0000FFFF,
- COLORREF crVLink = 0x00000000, const char *pccBack = NULL,
- const char *pccExtra = NULL);
- void htmlStartBODY(AElementPairList &eplBody);
- void htmlEndBODY(void) { outStringCRN("</BODY>"); }
-
- //a_Format: <pccTag pccExtra> {pccText} </pccTag>\n and similar
- void htmlStartTag(const char *pccTag, const char *pccExtra = NULL);
- void htmlStartTag(const char *pccTag, AElementPairList &eplTag);
- void htmlEndTag(const char *pccTag);
- void htmlDoTag(const char *pccTag, const char *pccText);
- void htmlDoTagEx(const char *pccTag, AElementPairList &eplTag, const char *pccText);
- void htmlDoTagEx(const char *pccTag, const char *pccExtra, const char *pccText);
-
- //a_Scripting support
- void htmlStartSCRIPT(const char *pccLanguage = NULL);
- void htmlEndSCRIPT();
-
- //a_Space skipping using control character
- void htmlDoSpace(int iN = 0x1);
-
- //a_The comment <!-- ... -->
- void htmlDoComment(const char *pcc1, const char *pcc2 = NULL, const char *pcc3 = NULL);
-
- //a_Helper functions
- void htmlDateTime(void)
- {
- time_t ttNow = time(NULL);
- outString(ctime(&ttNow));
- }
-
- //a_Generic MIME output
- void mimeOut(const char *pccType, const char *pccSubType);
- //a_MIME directive for HTML output with HTTP Cookie support
- void mimeHTML(ACookie *paCookie = NULL);
- //a_Only callable by AXBitmap (so far) when outputing x-xbitmap
- void mimeXBitmap(void) { outStringN("Content-Type: image/x-xbitmap\xA\xA"); }
- };
-
- //a_Global redirection operators (to avoid declaration matching), uses polymorphism of AElement
- inline AStreamOutput &operator <<(AStreamOutput &ahtmlOut, const ABaseElement &abeSource)
- {
- abeSource.doOut(&ahtmlOut);
- return ahtmlOut;
- }
-
-
- ///////////////////////////////////////////////////////////////////////////////////
- // ACGI class, inherits HTML features from AHTML and Form processing from AFormList
- ///////////////////////////////////////////////////////////////////////////////////
- class ACGI : RTTI_VIRTUAL public AHTML, RTTI_VIRTUAL public AFormList
- {
- public:
- ACGI(ostream *posOut = NULL) : AHTML(posOut) {}
- virtual ~ACGI() {}
-
- //a_Declares debug/dump related functions
- #ifdef _DEBUG_DUMP_
- void dump(void); //a_Debugging dump, when _DEBUG_DUMP_ is set
- #endif
-
- void cgiEnvironmentDump(int iFullDump = 0x0);
-
- //a_Internet related
- DWORD cgiGetIP(const char *pccIP = NULL);
-
- //a_FORM elements (Action==NULL mean use this script name, self posting)
- //a_See a_predef for FORMINPUT_xxx types usable by other sources
- void cgiStartFORM(const char *pccAction = NULL, const char *pccMethod = NULL); //a_Simple FORM parameters
- void cgiStartFORM(AElementPairList &eplItems) { htmlStartTag("FORM", eplItems); } //a_Extended FORM parameters (using AHTML wrapper)
- void cgiDoFORMInput(int iType, AElementPairList &eplItems, const char *pccContent = NULL);
- void cgiDoFORMInput(int iType, const char *pccName, const char *pccValue = NULL, const char *pccContent = NULL);
- void cgiEndFORM(void) { outStringCRN("</FORM>"); } //a_End the FORM
-
- //a_Form related: POST method implies both POST and GET, whereas GET is only a GET
- int cgiGetFormItems(istream *pisInput = NULL); //a_Defaults to cin/QUERY_STRING if no istream is specified (usually not specified!)
- int cgiGetFormItems(const char *pccInput); //a_String based, input known (good for debugging)
- int cgiGetQueryStringItems(const char *pccInput = NULL); //a_Tried to get Query string items even in POST (works!)
-
- //a_Checking functions
- int cgiIsGET(void) { return (strstr(cgiGetRequestMethod(), "GET") == NULL ? 0x0 : 0x1); }
- int cgiIsPOST(void) { return (strstr(cgiGetRequestMethod(), "POST") == NULL ? 0x0 : 0x1); }
-
- //a_Valid input checking
- int cgiIsValidEMail(const char *pccTest);
- int cgiIsWithoutMetaChar(const char *pccTest, int iStrict = 0x0);
- int cgiIsValidHTMLTag(const char *pccTest);
- int cgiIsValidURLProtocol(const char *pccTest);
- int cgiIsValidURL(const char *pccTest);
-
- //a_HTML specific checks and corrections
- int cgiIsNotValidHTMLLine(const char *pccTest); //a_Checks if it is a valid HTML line was entered in a form
- char *cgiValidateHTMLLine(char *pcLine); //a_Validates the HTML line by putting comment where invalid statement is
-
- //a_Outputing a URL after encoding
- void cgiEncodeAndOutputURL(const char *pccSource, int iLength = -0x1); //a_Encodes and outputs to the default stream
-
- //a_Get the desired environment variable, inlined for speed!
- const char *cgiGetSafeEnv(const char *pEnvName)
- {
- const char *pccValue = getenv(pEnvName);
- return (pccValue ? pccValue : NULL_STRING);
- }
-
- //a_HTTP specific
- const char *cgiGetHTTPReferer(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HTTP_REFERER");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHTTPUserAgent(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HTTP_USER_AGENT");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHTTPPragma(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HTTP_PRAGMA");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHTTPFrom(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HTTP_FROM");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHTTPAccept(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HTTP_ACCEPT");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHTTPCookie(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HTTP_COOKIE"); //a_Used with ACookie
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetAnnotationServer(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("ANNOTATION_SERVER");
- if (iOut) outStringN(pccX);
- return pccX;
- }
-
- //a_Path info
- const char *cgiGetPath(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("PATH");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetPathInfo(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("PATH_INFO");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetPathTranslated(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("PATH_TRANSLATED");
- if (iOut) outStringN(pccX);
- return pccX;
- }
-
- //a_Server/Gateway variables
- const char *cgiGetServerSoftware(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("SERVER_SOFTWARE");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetServerName(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("SERVER_NAME");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetServerPort(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("SERVER_PORT");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetServerProtocol(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("SERVER_PROTOCOL");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetGatewayInterface(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("GATEWAY_INTERFACE");
- if (iOut) outStringN(pccX);
- return pccX;
- }
-
- //a_Remote specific variable
- const char *cgiGetRemoteHost(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("REMOTE_HOST");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetRemoteAddress(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("REMOTE_ADDR");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetRemoteIdent(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("REMOTE_IDENT");
- if (iOut) outStringN(pccX);
- return pccX;
- }
-
- //a_Request variables
- const char *cgiGetContentType(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("CONTENT_TYPE");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetContentLength(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("CONTENT_LENGTH");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetRequestMethod(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("REQUEST_METHOD");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetQueryString(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("QUERY_STRING");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetScriptName(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("SCRIPT_NAME");
- if (iOut) outStringN(pccX);
- return pccX;
- }
-
- //a_System specific variables
- const char *cgiGetSHLVL(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("SHLVL");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetPWD(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("PWD");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetLogName(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("LOGNAME");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetUser(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("USER");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHost(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HOST");
- if (iOut) outStringN(pccX);
- return pccX;
- }
- const char *cgiGetHostType(int iOut = 0x0)
- {
- const char *pccX = cgiGetSafeEnv("HOSTTYPE");
- if (iOut) outStringN(pccX);
- return pccX;
- }
-
- protected:
- int _cgiDoFormItems(istream *pisInput = NULL); //a_The main function for form item retrieval
- void _cgiDoInputType(int iType); //a_Output FORM items TYPE
- };
-
-
- ///////////////////////////////////////////////////////////////////////////////////
- // AXBitmap - X11 bitmap generating class
- ///////////////////////////////////////////////////////////////////////////////////
- class AXBitmap : public ACGI
- {
- public:
- AXBitmap(ostream *posOut = NULL);
- virtual ~AXBitmap() {}
-
- //a_Declares debug/dump related functions
- #ifdef _DEBUG_DUMP_
- void dump(void); //a_Debugging dump, when _DEBUG_DUMP_ is set
- #endif
-
- //a_Size stuff for output of ABitArray only, ABitMatrix has it's size
- void xbmSetSize(int iNewH, int iNewW)
- {
- //a_NOTE: m_iHeight * m_iWidth ==(must)== ABitArray.m_iLength
- m_iHeight = iNewH;
- m_iWidth = iNewW;
- }
- void xbmSetHotSpot(int iX = -1, int iY = -1)
- {
- //a_Hot spot is not really too useful, but here for completeness
- m_iHotX = iX;
- m_iHotY = iY;
- }
- int xbmGetHeight(void) { return m_iHeight; }
- int xbmGetWidth(void) { return m_iWidth; }
- int xbmGetHotX(void) { return m_iHeight; }
- int xbmGetHotY(void) { return m_iWidth; }
-
- //a_Wrapper functions for bitmaps from ABitArray or ABitMatrix
- void xbmDoBitmap(ABitArray &baBitmap);
- //a_Available only when RTTI is available
- #ifdef __RTTI__
- void xbmDoBitmap(ABitMatrix &bmBitmap);
- #endif
-
- protected:
- //a_Do image/x-xbitmap '#define ...' header from dimensions and hotspot
- void _xbmDoXHeader(void);
-
- int m_iHeight, m_iWidth;
- int m_iHotX, m_iHotY;
- };
-